﻿using System.Collections.Generic;

namespace Commons.Models.Stats
{
	public class StatsGroup : IStatsGroup
	{
		protected IDictionary<string, IStat> stats = new Dictionary<string, IStat>();

		public StatsGroup()
		{
		}

		public StatsGroup(IEnumerable<IStat> stats)
		{
			foreach (IStat stat in stats)
			{
				AddStat(stat);
			}
		}

		public void AddStat(IStat stat)
		{
			stats.Add(stat.Definition.Unit.Name, stat);
		}

		public T GetStat<T>(IStatUnit unit) where T : IStat
		{
			IStat result;
			if (stats.TryGetValue(unit.Name, out result))
			{
				return (T) result;
			}
			throw new KeyNotFoundException("Stat Not Found= " + unit.Name);
		}

		public ICollection<IStat> GetStats()
		{
			return stats.Values;
		}

		public void Apply()
		{
			foreach (IStat stat in stats.Values)
			{
				stat.Apply();
			}
		}

		public bool HasChanges()
		{
			foreach (IStat stat in stats.Values)
			{
				if (stat.HasChanges())
				{
					return true;
				}
			}
			return false;
		}
    }
}
